package com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.repository;

import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.entity.AssessmentVulnerability;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.entity.PopulationEvaluateResult;
import com.css.fxfzxczh.modules.fastDisasterAnalysis.casualties.param.EvaluateParam;
import com.css.fxfzxczh.modules.remoteSenseData.vo.RemoteSenseVO;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xzy
 * @CreateTime: 2024/7/15 17:22
 * @Description:
 */
@Repository
public class AssessmentVulnerabilityRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    public List<AssessmentVulnerability> getAssessmentVulnerability(EvaluateParam evaluateParam) {
//        String sql = "select * from xczh_assessment_vulnerability where xsem_id='"+xsemId+"'";
//        List<AssessmentVulnerability> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AssessmentVulnerability.class));
//        return PlatformObjectUtils.isNotEmpty(query) && query.size()>0?query:new ArrayList<>();

       String sql="select * from xczh_assessment_vulnerability where xsem_id = ? and building_version = ?  " +
               "and population_version = ? order by prefecture,structure_type,destroy";
        List<AssessmentVulnerability> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(AssessmentVulnerability.class),
                evaluateParam.getXsemId(), evaluateParam.getBuildingVersion(), evaluateParam.getPopulationVersion());
        return list;
    }


    public void batchSave(List<AssessmentVulnerability> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "insert into xczh_assessment_vulnerability values(:id,:province,:city,:prefecture,:structureType," +
                ":destroy,:six,:seven,:eight,:nine,:ten,:xsemId,:sign,:buildingVersion,:populationVersion)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

}
